package com.cebbank.yyi.springbootapp.filter;

import org.apache.ibatis.plugin.Invocation;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: yyi
 * @version: 1.0
 **/

public class SqlMapperFilterChain {

    private final List<SqlMappersFilter> filters = new ArrayList<>();

    private int index = 0;

    public SqlMapperFilterChain(List<SqlMappersFilter> list) {
        filters.addAll(list);
    }
    public Object doFilter(Invocation invocation) throws InvocationTargetException, IllegalAccessException {
        if (index < filters.size()) {
            SqlMappersFilter sqlMapperFilter = filters.get(index);
            index++;
            return sqlMapperFilter.doFilter(invocation, this);
        } else {
            return invocation.proceed();
        }
    }
}
